package cn.domypay.mail.base;

import java.security.Security;
import java.util.List;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.URLName;
import cn.domypay.mail.bean.Mailbox;
import cn.domypay.mail.conf.DomypayMail;
import com.sun.mail.smtp.SMTPTransport;

public class SmtpBiz {
	
	private final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
	
	public SmtpBiz()
	{}
	/**
	 * 处理普通的SMTP协议
	 * @param mailbox
	 * @return
	 */
	public Properties getSmtpProp(Mailbox mailbox)
	{
		Properties properties = new Properties();
		properties.put("mail.smtp.host", mailbox.getSmtpHost());
		if(DomypayMail.mail_no_Auth.equals(mailbox.getIsAuth()))
		{
			mailbox.setIsAuth("false");
		}else{
			mailbox.setIsAuth("true");
		}
		properties.put("mail.smtp.auth", mailbox.getIsAuth());
		if("".equals(mailbox.getSmtpPort()) || null == mailbox.getSmtpPort())
		{
			mailbox.setSmtpPort(DomypayMail.DEFAULT_SMTP_PORT);
		}
		properties.put("mail.smtp.port", Integer.parseInt(mailbox.getSmtpPort()));
		properties.put("mail.smtp.connectiontimeout", DomypayMail.DEFAULT_CONNECTION_TIMEOUT);
		
		return properties;
	}
	
	/**
	 * 处理基于ssl连接方式的smtp协议
	 * @param mailbox
	 * @return
	 */
	public Properties getSmtp_SslProp(Mailbox mailbox)
	{
		Properties properties=new Properties();
		properties.put("mail.smtp.host", mailbox.getSmtpHost());
		properties.put("mail.smtp.socketFactory.class",SSL_FACTORY);
		properties.put("mail.smtp.socketFactory.fallback", false);
		
		if(DomypayMail.mail_no_Auth.equals(mailbox.getIsAuth()))
		{
			mailbox.setIsAuth("false");
		}else{
			mailbox.setIsAuth("true");
		}
		properties.put("mail.smtp.auth", mailbox.getIsAuth());
		if("".equals(mailbox.getSmtpPort()) || null == mailbox.getSmtpPort())
		{
			mailbox.setSmtpPort(DomypayMail.DEFAULT_SMTP_SSL_PORT);
		}
		properties.put("mail.smtp.port", Integer.parseInt(DomypayMail.DEFAULT_SMTP_SSL_PORT));
		properties.put("mail.smtp.socketFactory.port", Integer.parseInt(DomypayMail.DEFAULT_SMTP_SSL_PORT));

		properties.put("mail.smtp.connectiontimeout", DomypayMail.DEFAULT_CONNECTION_TIMEOUT);
		
		return properties;
	}
	/**
	 * 通过用户邮箱配置信息，获取url访问名
	 * @param mailbox
	 * @return
	 */
	public URLName getSmtpURLName(Mailbox mailbox)
	{
		if("".equals(mailbox.getSmtpPort()) || null == mailbox.getSmtpPort())
		{
			if(DomypayMail.NEED_SSL.equals(mailbox.getSmtpSsl()))
			{
				mailbox.setSmtpPort(DomypayMail.DEFAULT_SMTP_SSL_PORT);
			}else{
				mailbox.setSmtpPort(DomypayMail.DEFAULT_SMTP_PORT);
			}
		}
			
		URLName name=new URLName("smtp",mailbox.getSmtpHost(),Integer.parseInt(mailbox.getSmtpPort()),null,mailbox.getUsername(),mailbox.getPassword());
		return name;
	}
	
	public List checkSmtp(Session session,Mailbox mailbox) throws MessagingException
	{
		Transport transport;
		if(DomypayMail.NEED_SSL.equals(mailbox.getSmtpSsl()))
		{
			//进行ssl连接的认证���֤
			Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
		}
		//进行普通认证������ͨ��֤
		transport = new SMTPTransport(session,getSmtpURLName(mailbox));
			
		transport.connect();
		transport.close();
		return null;
	}
	
	/**
	 * 用来发送单封邮件
	 * @param session
	 * @param mail
	 * @param mailbox
	 * @return
	 * @throws MessagingException 
	 */
	public boolean smtpSender(Session session,Message mail,Mailbox mailbox) throws MessagingException
	{
		Transport transport;
		if(DomypayMail.NEED_SSL.equals(mailbox.getSmtpSsl()))
		{
			//进行ssl连接的认证���֤
			Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
		}			
		transport=new SMTPTransport(session,getSmtpURLName(mailbox));
		
		transport.connect();
			
		mail.saveChanges();
			
		transport.sendMessage(mail, mail.getAllRecipients());
			
		transport.close();
			
		return true;
	}
	
	/**
	 * 打开邮件smtp发送连接
	 * @throws MessagingException 
	 */
	public Transport openSmtpConnection(Session session,Mailbox mailbox) throws MessagingException
	{
		
		Transport transport;
		
		if(DomypayMail.NEED_SSL.equals(mailbox.getSmtpSsl()))
		{
			//进行ssl连接的认证���֤
			Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
		}			
		transport=new SMTPTransport(session,getSmtpURLName(mailbox));
		
		transport.connect();
		
		return transport;
	}
	/**
	 * 用于多线程发送邮件使用
	 * 
	 * at first: openSmtpConnection
	 * 
	 * and then: for(){
	 * 	smtpSenderInThread
	 * }
	 *
	 * last:closeSmtpConnection
	 * 
	 * @param transport
	 * @param mail
	 * @return
	 * @throws MessagingException 
	 */
	public boolean smtpSenderInThread(Transport transport,Message mail) throws MessagingException
	{
		transport.sendMessage(mail, mail.getAllRecipients());
		return true;
	}
	/**
	 * 关闭邮件smtp端口
	 * @throws MessagingException 
	 */
	public void closeSmtpConnection(Transport transport) throws MessagingException
	{
		if(transport.isConnected())
		{
			transport.close();
		}
	}
}
